<template>
  <a-layout>

    <a-layout>
      <a-layout>
        <!-- 内容 -->
        <a-layout-content class="content">
          <h3 v-if="eDocTree.length === 0">对不起，找不到相关文档！</h3>
          <a-row>
            <a-col :span="6">
              <a-tree
                v-if="eDocTree.length > 0"
                :tree-data="eDocTree"
                @select="onSelect"
                :fieldNames="{ title: 'name', key: 'id', children: 'children' }"
                :defaultExpandAll="true"
              >
              </a-tree>
            </a-col>
            <a-col :span="18" v-if="doc">
              <div>
                <h2>{{ doc.name }}</h2>
                <div>
                  <span>阅读数：{{ doc.viewCount }}</span> &nbsp; &nbsp;
                  <span>点赞数：{{ doc.voteCount }}</span>
                </div>
                <a-divider style="height: 2px; background-color: #9999cc" />
              </div>
              <div class="wangeditor" :innerHTML="valueHtml"></div>
              <div class="vote-div">
                <a-button
                  type="primary"
                  shape="round"
                  :size="'large'"
                  @click="vote"
                >
                  <template #icon>
                    <LikeOutlined /> &nbsp;点赞：{{ doc.voteCount }}
                  </template>
                </a-button>
              </div>
            </a-col>
          </a-row>
        </a-layout-content>
      </a-layout>
    </a-layout>
  
  </a-layout>
</template>

<script setup lang="ts">
import { useApi } from "@/api/index";
import { onMounted, ref } from "vue";

import { LikeOutlined } from "@ant-design/icons-vue";
const defaultSelectedKeys = ref();
defaultSelectedKeys.value = [];

// 当前选中的文档
const valueHtml = ref();
const api = useApi();

const route = useRoute();

import { useRoute } from "vue-router";

import { Tool } from "@/utils/tool";

import useDoc from "@/store/docStore";
import { storeToRefs } from "pinia";
const docStore = useDoc();

onMounted(() => {
  docStore.getDocs(route.query.ebookId as unknown as number);
});
/**
 *  当前电子书文档数据查询
 **/

const { docTree: eDocTree } = storeToRefs(docStore);
const { doc } = storeToRefs(docStore);

/**
 * 内容查询内容加载
 **/
const handleQueryContent = async (id: number) => {
  //如果不清空数据,则编辑保存重新加载数据后,再点击编辑,数据会重复
  // eDocTree.value = [];
  const data = await api.getDocContent(id);

  valueHtml.value = data;
};

const onSelect = (selectedKeys: any, info: any) => {
  console.log("selected", selectedKeys, info);
  if (Tool.isNotEmpty(selectedKeys)) {
    // 选中某一节点时，加载该节点的文档信息
    doc.value = info.selectedNodes[0];
    // 加载内容
    handleQueryContent(selectedKeys[0]);
  }
};

// 点赞
const vote = () => {
  const data = api.DocVote_URL(doc.value?.id!);
  if (data != null) {
    if (doc.value !== undefined && doc.value !== null) {
      doc.value.voteCount += 1;
    } else {
      // 处理 doc.value 为 undefined 或 null 的情况，例如初始化
    }
  }
};
</script>

<style scoped>
.content {
  height: 100vh;
}
/* table 样式 */
.wangeditor table {
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
}
.wangeditor table td,
.wangeditor table th {
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  padding: 3px 5px;
}
.wangeditor table th {
  border-bottom: 2px solid #ccc;
  text-align: center;
}

/* blockquote 样式 */
.wangeditor blockquote {
  display: block;
  border-left: 8px solid #d0e5f2;
  padding: 5px 10px;
  margin: 10px 0;
  line-height: 1.4;
  font-size: 100%;
  background-color: #f1f1f1;
}

/* code 样式 */
.wangeditor code {
  display: inline-block;

  background-color: #f1f1f1;
  border-radius: 3px;
  padding: 3px 5px;
  margin: 0 3px;
}

.wangeditor pre code {
  display: block;
}

/* ul ol 样式 */
.wangeditor ul,
ol {
  margin: 10px 0 10px 20px;
}

/* 和antdv p冲突，覆盖掉 */
.wangeditor blockquote p {
  font-family: "YouYuan";
  margin: 20px 10px !important;
  font-size: 16px !important;
  font-weight: 600;
}

/* 点赞 */
.vote-div {
  padding: 15px;
  text-align: center;
}

/* 图片自适应 */
.wangeditor img {
  max-width: 100%;
  height: auto;
}

/* 视频自适应 */
.wangeditor iframe {
  width: 100%;
  height: 400px;
}
</style>
